import axios from 'axios'

// 创建axios实例
const api = axios.create({
  baseURL: '/api',
  timeout: 10000
})

// 请求拦截器
api.interceptors.request.use(
  config => {
    const token = localStorage.getItem('adminToken')
    if (token) {
      config.headers.Authorization = `Bearer ${token}`
    }
    return config
  },
  error => {
    return Promise.reject(error)
  }
)

// 响应拦截器
api.interceptors.response.use(
  response => {
    return response.data
  },
  error => {
    if (error.response?.status === 401) {
      localStorage.removeItem('adminToken')
      localStorage.removeItem('level')
      window.location.href = '/login'
    }
    return Promise.reject(error)
  }
)

// 创建活动
export const createActivity = (data) => {
  return api.post('/activity/create', data)
}

// 获取活动列表
export const getActivityList = () => {
  return api.get('/activity/list')
}

// 获取活动详情
export const getActivityById = (id) => {
  return api.get(`/activity/${id}`)
}

// 获取管理员活动列表
export const getAdminActivityList = () => {
  return api.get('/activity/admin/list')
}

// 更新活动
export const updateActivity = (data) => {
  return api.put('/activity/update', data)
}

// 删除活动
export const deleteActivity = (id) => {
  return api.delete(`/activity/${id}`)
}

// 上传活动封面
export const uploadActivityCover = (id, file) => {
  const formData = new FormData()
  formData.append('cover', file)
  
  return api.post(`/activity/${id}/uploadCover`, formData, {
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })
} 